package com.jike.noobmoney.mvp.view.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.entity.NewTaskListEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/3/28
 * 新手任务
 */

public class NewTaskActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_money)
    TextView tvMoney;

    @BindView(R.id.tv_new_yaoqing)
    TextView tvNewYaoqing;
    @BindView(R.id.tv_new_shipin)
    TextView tvNewShipin;
    @BindView(R.id.tv_new_gongzhonghao)
    TextView tvNewGongzhonghao;
    @BindView(R.id.tv_new_renwu)
    TextView tvNewRenwu;
     @BindView(R.id.tv_title)
     TextView tv_Title;
    @BindView(R.id.tv_newtask3)
    TextView tvNewRenwu3;
    @BindView(R.id.tv_newtask6)
    TextView tvNewRenwu6;

    @BindView(R.id.tv_new_jinqun)
    TextView tvNewJinqun;
    @BindView(R.id.tv_invite_num)
    TextView tvInviteNum;


    private UserPresenter userPresenter;
    private String userid;

    private int invite = -1;
    private int video = -1;
    private int attention = -1;
    private int complete = -1;
    private int team = -1;
    private int complete3;
    private int complete6;



    @Override
    protected int layoutResID() {
        return R.layout.activity_new_task;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        tv_Title.setText("新手任务");
        refreshData();
        newTaskList();
    }


    /**
     * 获取余额
     */
    private void refreshData() {
        showProgress();
        userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
    }

    /**
     * 获取新手任务列表
     */
    private void newTaskList() {
        showProgress();
        userPresenter.newusertasklist(userid, ConstantValue.RequestKey.new_task_list);
    }

    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }

    @OnClick({R.id.tv_new_yaoqing, R.id.tv_new_shipin, R.id.tv_new_gongzhonghao, R.id.tv_new_renwu, R.id.tv_new_jinqun
            , R.id.tv_newtask3, R.id.tv_newtask6
            , R.id.iv_video, R.id.iv_qqqun, R.id.iv_gzh,R.id.iv_back
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_new_yaoqing:
                switch (invite) {
                    case 0:
                        //未完成  去邀请好友
                        recommend();
                        break;
                    case 1:
                        //完成
                        lingqu("1");
                        break;
                }
                break;
            case R.id.tv_new_shipin:
                switch (video) {
                    case 0:
                        //未完成  看视频
                        Intent intent = new Intent(context, VideoTwoActivity.class);
                        intent.putExtra("isTask", true);
                        startActivityForResult(intent, 201);
                        break;
                    case 1:
                        //完成
                        lingqu("2");
                        break;
                }
                break;
            case R.id.tv_new_gongzhonghao:
                switch (attention) {
                    case 0:
                        //未完成
                        GuanzhuActivity.startAct(this, 1);
                        break;
                    case 1:
                        //完成
                        lingqu("3");
                        break;
                }
                break;
            case R.id.tv_new_renwu:
                switch (complete) {
                    case 0:
                        //未完成
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case 1:
                        //完成
                        lingqu("4");
                        break;
                }
                break;

            case R.id.tv_newtask3:
                switch (complete3) {
                    case 0:
                        //未完成
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case 1:
                        //完成
                        lingqu("6");
                        break;
                }
                break;

            case R.id.tv_newtask6:
                switch (complete6) {
                    case 0:
                        //未完成
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case 1:
                        //完成
                        lingqu("7");
                        break;
                }
                break;
            case R.id.tv_new_jinqun:
                switch (team) {
                    case 0:
                        //未完成
                        GuanzhuActivity.startAct(this, 2);
                        break;
                    case 1:
                        //完成
                        lingqu("5");
                        break;
                }
                break;


            //----------------------------------------------------------------------
            case R.id.iv_video:
                //新手教程voide
                Intent intent = new Intent(context, NewTeachActivity.class);
                intent.putExtra("isTask", false);
                startActivity(intent);
                break;
            case R.id.iv_qqqun:
                //QQ群
                ComUtils.startQQ(context, 3, "702911982");
                break;
            case R.id.iv_gzh:
                //GuanzhuActivity.startAct(this, 3);
               // ComUtils.startQQ(context, 3, "739414363");
                ComUtils.startQQ(context, 3, "736856485");
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode);//签到页
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.open();
    }


    private void lingqu(String flag) {
        if (AppUtils.isFastClick()) {
            ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
            return;
        }
        showProgress();
        userPresenter.newusertaskreceive(userid, flag, ConstantValue.RequestKey.new_task_receive);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());
        }

        //0是未完成  1是完成  2是已领取
        if (ConstantValue.RequestKey.new_task_list.equals(actionType)) {
            NewTaskListEntity newTaskListEntity = (NewTaskListEntity) data;
            invite = newTaskListEntity.getInvite();
            video = newTaskListEntity.getVideo();
            attention = newTaskListEntity.getAttention();
            complete = newTaskListEntity.getComplete();
            team = newTaskListEntity.getTeam();
            complete3 = newTaskListEntity.getComplete2();
            complete6 = newTaskListEntity.getComplete3();

            refreshNewView(tvNewYaoqing, invite);
            refreshNewView(tvNewShipin, video);
            refreshNewView(tvNewGongzhonghao, attention);
            refreshNewView(tvNewRenwu, complete);
            refreshNewView(tvNewJinqun, team);
            refreshNewView(tvNewRenwu3, complete3);
            refreshNewView(tvNewRenwu6, complete6);
        }

        if (ConstantValue.RequestKey.new_task_receive.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            //领取成功
            newTaskList();
            refreshData();
        }

    }

    private void refreshNewView(TextView textView, int status) {
        switch (status) {
            case 0:
                //未完成
                textView.setSelected(true);
                textView.setText("去完成");
                break;
            case 1:
                //完成
                textView.setSelected(true);
                textView.setText("待领取");
                break;
            case 2:
                //已领取
                textView.setSelected(false);
                textView.setText("已领取");
                break;

        }
    }


    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 201 && resultCode == RESULT_OK) {
            newTaskList();
        }
    }
}
